﻿//***********************************************************************************
// 文件名称：TimePeriod
// 功能描述：时间周期类
// 数据表：
// 作者：liuxiaohu
// 日期：2017/3/13 9:13:20
// 修改记录：
//***********************************************************************************

using System;

namespace GameServer
{
    /// <summary>
    /// 时间区间类
    /// </summary>
    public class TimePeriod
    {
        #region 变量

        /// <summary>
        /// 开始时间
        /// </summary>
        private CustomTime beginDataTime;

        /// <summary>
        /// 结束时间
        /// </summary>
        private CustomTime finishDataTime;

        #endregion

        #region 方法

        /// <summary>
        /// 传入自定义开始和结束时间的构造函数
        /// </summary>
        /// <param name="beginYear">开始年份</param>
        /// <param name="beginMonth">开始月份</param>
        /// <param name="beginDay">开始天</param>
        /// <param name="beginDay">星期几开始</param>
        /// <param name="beginHour">开始时</param>
        /// <param name="beginMinute">开始分</param>
        /// <param name="beginSecond">开始秒</param>
        /// <param name="finishYear">结束年份</param>
        /// <param name="finishMonth">结束月份</param>
        /// <param name="finishDay">结束天</param>
        /// <param name="beginDay">星期几结束</param>
        /// <param name="finishHour">结束时</param>
        /// <param name="finishMinute">结束分</param>
        /// <param name="finishSecond">结束秒</param>
        public TimePeriod(Int32 beginYear, Int32 beginMonth, Int32 beginDay, Int32 beginWeekDay, Int32 beginHour, Int32 beginMinute, Int32 beginSecond
            , Int32 finishYear, Int32 finishMonth, Int32 finishDay, Int32 finishWeekDay, Int32 finishHour, Int32 finishMinute, Int32 finishSecond)
        {
            this.beginDataTime = new CustomTime(beginYear, beginMonth, beginDay, beginWeekDay, beginHour, beginMinute, beginSecond);
            this.finishDataTime = new CustomTime(finishYear, finishMonth, finishDay, finishWeekDay, finishHour, finishMinute, finishSecond);
        }

        /// <summary>
        /// 周期是否为常态
        /// </summary>
        /// <returns>常态返回true,否则返回false</returns>
        public Boolean IsNormal()
        {
            // 所有值都为-1，表示一直存在
            if (beginDataTime.Year == Const.DefaultValue && beginDataTime.Month == Const.DefaultValue &&
                beginDataTime.Day == Const.DefaultValue && beginDataTime.WeekDay == Const.DefaultValue &&
                beginDataTime.Hour == Const.DefaultValue && beginDataTime.Minute == Const.DefaultValue &&
                beginDataTime.Second == Const.DefaultValue)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 判断一个系统时间是否在当前时间周期内
        /// </summary>
        /// <param name="dataTime">系统时间</param>
        /// <returns>在返回true,否则返回false</returns>
        public Boolean IsContains(DateTime dataTime)
        {

            return false;
        }

        /// <summary>
        /// 判断系统时间是否已到达自定义的时间
        /// </summary>
        /// <param name="customTime">自定义时间</param>
        /// <param name="dataTime">系统时间</param>
        /// <returns>已到达返回true,否则返回false</returns>
        private Boolean IsArrivedTime(CustomTime customTime, DateTime dataTime)
        {
            if (customTime.Year == -1 || dataTime.Year == customTime.Year)
            {
                // 已到当前年
                if (customTime.Month == -1 || dataTime.Month == customTime.Month)
                {
                    // 已到当前月
                    if ((customTime.Day == -1 && customTime.WeekDay == -1) || (customTime.WeekDay == -1 && dataTime.Day == customTime.Day) || (customTime.Day == -1 && (Int32)dataTime.DayOfWeek == customTime.WeekDay))
                    {
                        // 已到当前天(每月的某一天或每周的星期几)
                        if (customTime.Hour == -1 || dataTime.Hour == customTime.Hour)
                        {
                            // 已到当前小时
                            if (customTime.Minute == -1 || dataTime.Minute == customTime.Minute)
                            {
                                // 已到当前分
                                if (customTime.Second == -1 || dataTime.Second == customTime.Second)
                                {
                                    // 已到当前秒 
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 判断一个系统时间是否已到开启时间
        /// </summary>
        /// <param name="dataTime">系统时间</param>
        /// <returns>已到则返回true,否则返回false</returns>
        public Boolean IsOpen(DateTime dataTime)
        {
            if (IsNormal())
            {
                return true;
            }
            else
            {
                return IsArrivedTime(beginDataTime, dataTime);
            }
        }

        /// <summary>
        /// 判断一个系统时间是否已到关闭时间
        /// </summary>
        /// <param name="dataTime">系统时间</param>
        /// <returns>已到则返回true,否则返回false</returns>
        public Boolean IsClose(DateTime dataTime)
        {
            if (IsNormal())
            {
                return false;
            }
            else
            {
                return IsArrivedTime(finishDataTime, dataTime);
            }
        }

        #endregion
    }
}
